package com.qf.controller;

import com.qf.entity.Classsify;
import com.qf.entity.Dept;
import com.qf.entity.Income;
import com.qf.entity.User;
import com.qf.enums.OAExceptionEnum;
import com.qf.service.ClasssifyService;
import com.qf.service.DeptService;
import com.qf.service.IncomeService;
import com.qf.service.UserService;
import com.qf.utils.R;
import com.qf.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author 程云
 * @Date 2019/12/11 10:52
 * 人生在世 代码二字
 */

/**
 * 收入证明
 */
@Controller
@RequestMapping("/resource")
@Slf4j
public class IncomeController {

    @Autowired
    private DeptService deptService;

    @Autowired
    private ClasssifyService classsifyService;

    @Autowired
    private UserService userService;

    @Autowired
    private IncomeService incomeService;

    //访问收入证明流程页面
    @GetMapping("/renshi/demo3/list")
    public String incomeUI(Model model){
        //获取登录对象信息
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        model.addAttribute("user",user);
        //部门
        List<Dept> list = deptService.findAllDept();
        model.addAttribute("list",list);
        //职务
        List<Classsify> list1 = classsifyService.findAllClass();
        model.addAttribute("list1",list1);
        //获取所有管理信息
        List<User> userList = userService.findAllUser();
        model.addAttribute("userList",userList);
        return "resource/renshi/demo3/list";
    }

    //添加收入证明流程
    @PostMapping("/add-income")
    @ResponseBody
    public ResultVO addIncome(@Valid Income income, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            String msg = bindingResult.getFieldError().getDefaultMessage();
            log.info("【收入证明流程】 参数错误 msg = {},income = {}",msg,income);
            return R.error(OAExceptionEnum.INFO_PARAM_ERROR.getCode(),msg);
        }
        Integer count = incomeService.add(income);
        if (count != 1){
            log.info("【添加失败】 收入流程添加失败 count = {}",count);
            return R.error(OAExceptionEnum.ADD_INCOME_ERROR.getCode(),OAExceptionEnum.ADD_INCOME_ERROR.getMsg());
        }
        return R.ok();
    }
}
